<?php
require_once(DIR_LIBRARY."orm".DS."ctp".DS."ctp.php");
class Ctp_Index_Model extends NICED_Model
{
    /**
     * @param $app int
     * @param $type int, thread type in app
     */
    public function createOneThread($app, $type, $user_id, $title, $body)
	{
        $meta = self::buildMeta(array("title" => $title, "body" => $body));

        $thread = $this->createThread($app, $user_id, $app);
        $post   = $this->createPost($thread, $user_id, $meta);

		return true;
	}

    public function updateOneThread($app, $sequence, $title, $body)
    {
        $meta = self::buildMeta(array("title" => $title, "body" => $body));

        $this->updateThread($app, $sequence);
        $thread = $this->getThread($app, $sequence);
        $this->updatePost($thread, 1, $meta);
    }

    public function deleteOneThread($app, $sequence)
    {
        $bool = $this->deleteThread($app, $sequence);
        $thread = $this->getThread($app, $sequence);
        //$bool = $this->deletePost($thread, 1);
    }

    public function createThread($app, $user_id, $type)
    {
        $thread = CtpThread::factory();
        $thread->setApp($app);
        $thread->setType($type);
        $thread->setCreator($user_id);
        $bool = $thread->create();
        $this->set("thread", $thread);

        return $thread;
    }

    public function updateThread($app, $sequence)
    {
        $thread = CtpThread::factory();
        $bool = $thread->update($app, $sequence);
        $this->set("thread", $thread);

        return $bool;
    }

    public function deleteThread($app, $sequence)
    {
        $thread = CtpThread::factory();
        $bool = $thread->delete($app, $sequence);
        $this->set("thread", $thread);

        return $bool;
    }

    public function getThread($app, $sequence)
    {
        $thread = CtpThread::factory();
        $thread->getThread($app, $sequence);
        $this->set("thread", $thread);

        return $thread;
    }

    public function createPost($thread, $user_id, $meta)
    {
        $post  = CtpPost::factory();
        $post->setBody($meta["body"]);
        $post->setCreator($user_id);
        $post->setMeta(serialize($meta));
        $bool = $post->create($thread);
        $this->set("post", $post);

        return $post;
    }

    public function updatePost($thread, $sequence, $meta)
    {
        $post = CtpPost::factory();
        $post->setBody($meta["body"]);
        $post->setMeta(serialize($meta));
        $bool = $post->update($thread, $sequence);
        $this->set("post", $post);

        return $bool;
    }

    public function deletePost($thread, $sequence)
    {
        $post = CtpPost::factory();
        $bool = $post->delete($thread, $sequence);
        $this->set("post", $post);

        return $bool;
    }

    public function getPost($thread, $sequence)
    {
        $post  = CtpPost::factory();

        return $post->getPost($thread, $sequence);
    }

    public static function buildMeta($data)
    {
        return array("title" => $data["title"], "body" => $data["body"]);
    }
}//end class
